﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace atm_qlda
{
    public partial class Chuyentien : Form
    {
        private SqlConnection conn;
        private SqlCommand cmd, cmd1, cmd2;
        string s;
        public Chuyentien(string sodu)
        {
            InitializeComponent();
            label5.Text = sodu;
        }

        private void Chuyentien_Load(object sender, EventArgs e)
        {
            string con = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
            conn = new SqlConnection(con);
            cmd = new SqlCommand("select Balance from UserAcc where IDCard='" + label5.Text + "'", conn);
            conn.Open();

            SqlDataReader reader = cmd.ExecuteReader();
            reader.Read();
            s = reader.GetValue(0).ToString();
            textBox1.Text = s;
            conn.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox2.Text = textBox2.Text + button3.Text;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            textBox2.Text = textBox2.Text + button4.Text;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            textBox2.Text = textBox2.Text + button5.Text;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            textBox2.Text = textBox2.Text + button7.Text;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            textBox2.Text = textBox2.Text + button8.Text;
        }

        private void button9_Click(object sender, EventArgs e)
        {
            textBox2.Text = textBox2.Text + button9.Text;
        }

        private void button10_Click(object sender, EventArgs e)
        {
            textBox2.Text = textBox2.Text + button10.Text;
        }

        private void button11_Click(object sender, EventArgs e)
        {
            textBox2.Text = textBox2.Text + button11.Text;
        }

        private void button12_Click(object sender, EventArgs e)
        {
            textBox2.Text = textBox2.Text + button12.Text;
        }

        private void button13_Click(object sender, EventArgs e)
        {
            textBox2.Text = textBox2.Text + button13.Text;
        }

        private void button14_Click(object sender, EventArgs e)
        {
            textBox2.Text = "";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox2.Text == "" || textBox3.Text == "")
            {
                MessageBox.Show("Chua nhap day du thong tin");
            }
            else
            {
                if (textBox3.Text == label5.Text)
                {

                    MessageBox.Show("Khong the chuyen cho chinh minh");
                }
                else
                {


                    cmd = new SqlCommand("select IDCard from UserAcc where IDCard='" + textBox3.Text + "' ", conn);
                   conn.Open();
                    SqlDataReader rd = cmd.ExecuteReader();
                   
                        if (rd.Read())
                        {

                            conn.Close();
                            if (float.Parse(textBox1.Text) - float.Parse(textBox2.Text) < 0)
                            {
                                MessageBox.Show("Tai khoan cua ban khong du");

                                textBox2.Text = "";
                                textBox3.Text = "";
                            }
                            else
                            {
                                if (MessageBox.Show("Bạn có chắc chắn muốn chuyển không ?", "Thoát", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                                {
                                    textBox4.Text = (float.Parse(textBox1.Text) - float.Parse(textBox2.Text)).ToString();
                                    cmd1 = new SqlCommand("update UserAcc set Balance='" + textBox4.Text + "' where IDCard='" + label5.Text + "'", conn);
                                    cmd2 = new SqlCommand("update UserAcc set Balance=Balance+'" + textBox2.Text + "' where IDCard='" + textBox3.Text + "'", conn);
                                    conn.Open();
                                    cmd1.ExecuteNonQuery();
                                    cmd2.ExecuteNonQuery();
                                    
                                    MessageBox.Show("Chuyen tien thanh cong");
                                    conn.Close();
                                }
                            }
                        


                    }
                    else 
                    {
                        MessageBox.Show("Tai khoan khong ton tai");
                        this.Hide();
                        if (MessageBox.Show("Bạn có giao dịch nữa không ?", "Thoát", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {
                            Chuyentien f = new Chuyentien(label5.Text);
                            f.Show();

                        }
                        else {
                            Choose f = new Choose();
                            f.Show();
                        }
                    }
                }
            }
        
        }

        private void button6_Click(object sender, EventArgs e)
        {

        }
    }
}
